﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089
{
    public class Slide : ComponentView
    {
        private float _minValue = 0;
        private float _currentValue;
        private float _maxValue = 100;
        private Color _titleColor;
        private Button _buttonDrag;
        private ImageView _slide;

        private ComponentView _panel;

        private bool isLeftMouseClicked = false;
        private Vector2 _lastMousePosition = Vector2.Zero;
        private Vector2 _fullSizeOfSlide;

        public float CurrentValue
        {
            get { return _currentValue; }
            set
            {
                _currentValue = value;
                if (_currentValue < _minValue)
                    _currentValue = _minValue;
                if (_currentValue > _maxValue)
                    _currentValue = _maxValue;
                setSlideValue();
            }
        }

        public Slide(int minvalue, int maxvalue, Vector2 position, int width, Color color, float alpha)
        {
            Position = position;
            _titleColor = color;

            _minValue = minvalue;
            _maxValue = maxvalue;

            _buttonDrag = new Button(@"Resources/my_circle_button", new Vector2(0, 0), 1);
            Size = new Vector2(width, _buttonDrag.Size.Y);
            _buttonDrag.Position = new Vector2(this.Size.X - _buttonDrag.Size.X + 2, 0);

            _slide = new ImageView(@"Resources/bar", new Vector2(5, _buttonDrag.Size.Y / 2 - 5), 1);
            _slide.ColorForDraw = Color.GreenYellow;
            _fullSizeOfSlide = _slide.Size;

            _panel = new ComponentView(@"Resources/slide_background", new Vector2(0, 0), Size);
            _panel.ColorForDraw = Color.Snow;

            _panel.addToSubView(_slide);
            _panel.addToSubView(_buttonDrag);

            this.addToSubView(_panel);

            CurrentValue = 100;
            setSlideValue();
        }

        public void setSlideValue()
        {
            _slide.Size = new Vector2(_fullSizeOfSlide.X * (_currentValue / _maxValue), _slide.Size.Y);
        }

        public override void draw(GameTime gameTime, SpriteBatch spriteBatch, Color color)
        {
            base.draw(gameTime, spriteBatch, color);

            _panel.draw(gameTime, spriteBatch, color);
        }

        public override void update(GameTime gameTime)
        {
            base.update(gameTime);

            _panel.update(gameTime);

            #region Mouse processing
            MouseState ms = Mouse.GetState();
            Vector2 mousePosition = new Vector2(ms.X, ms.Y);
            if (ms.LeftButton == ButtonState.Pressed && _buttonDrag.isContainPoint(mousePosition))
            {
                isLeftMouseClicked = true;
            }
            if (isLeftMouseClicked)
            {
                Vector2 newPos = _buttonDrag.Position;
                newPos.X = mousePosition.X - _buttonDrag.Size.X / 2;

                if (newPos.X < this.Position.X)
                    newPos.X = this.Position.X;
                float temp = this.Position.X + this.Size.X - _buttonDrag.Size.X + 2;
                if (newPos.X > temp)
                    newPos.X = temp;
                _buttonDrag.Position = newPos;

                // update currentValue
                CurrentValue = (_buttonDrag.Position.X - Position.X) / (this.Size.X - _buttonDrag.Size.X) * _maxValue;
            }
            if (ms.LeftButton == ButtonState.Released && isLeftMouseClicked)
            {
                isLeftMouseClicked = false;
            }
            _lastMousePosition = mousePosition;
            #endregion
        }
    }
}
